package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.gtm4.HangBlow;
import com.infoauto.opc.gateway.model.gtm4.Spray;
import com.infoauto.opc.gateway.service.SprayService;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 喷房
 */

@CrossOrigin
@RestController
@RequestMapping("/spray")
public class SprayController {
    @Autowired
    private SprayService service;

    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectByProducTionTimeAndShift(String productionTime,Integer shift){
        try{
            List<Spray> list = service.selectByProducTionTimeAndShift(productionTime,shift);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

   }
    /**
     * 喷房
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody Spray spray) {
        //2.调用service更新
        service.update(spray);
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/updateRemark", method = RequestMethod.PUT)
    public Result updateRemark(String id,String remark) {
        Spray spray = service.selectById(id);
        if(spray!=null){
            spray.setRemark(remark);
            spray.setModifyDateTime(new Date());
            //2.调用service更新
            service.update(spray);
        }
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response, String productionTime, Integer shift) throws Exception {
        //1.构造数据
        List<Spray> list = service.selectByProducTionTimeAndShift(productionTime,shift);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        String[] head1 = {"GTMC#4"};
        //3.构造sheet
        Sheet sheet = workbook.createSheet("喷房");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        //设置单元格样式
        //设置边框
        //创建单元格样式对象
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setBorderBottom(BorderStyle.NONE);//下边框
        cellStyle1.setBorderTop(BorderStyle.NONE);//上边框
        //设置字体
        Font font = workbook.createFont();//创建字体对象
        font.setFontName("宋体");//设置字体
        font.setFontHeightInPoints((short)16);//设置字号
        cellStyle1.setFont(font);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : head1) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle1);
        }
        //合并单元格
        CellRangeAddress region1 =new CellRangeAddress(0, 0, 0, 16);
        sheet.addMergedRegion(region1);
        AtomicInteger ai1 = new AtomicInteger(1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Cell cell = null;
        Row row2 = sheet.createRow(ai1.getAndIncrement());
        cell = row2.createCell(0);
        cell.setCellValue("Date");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row2.createCell(1);
        cell.setCellValue(productionTime);
        //设置样式
        cell.setCellStyle(cellStyle);
        sheet.setColumnWidth(1, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(2, 12 * 256);//设置第一列的宽度是31个字符宽度
//        sheet.setColumnWidth(3, 12 * 256);//设置第一列的宽度是31个字符宽度
//        sheet.setColumnWidth(4, 12 * 256);//设置第一列的宽度是31个字符宽度
//        sheet.setColumnWidth(5, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(4, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(7, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(10, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(13, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(14, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(16, 30 * 256);//设置第一列的宽度是31个字符宽度
        row2.setHeightInPoints(22);//设置行的高度是50个点
        AtomicInteger ai2 = new AtomicInteger(2);
        Row row3 = sheet.createRow(ai2.getAndIncrement());
        cell = row3.createCell(0);
        cell.setCellValue("Shift");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row3.createCell(1);
        cell.setCellValue(shift);
        //设置样式
        cell.setCellStyle(cellStyle);
        //合并单元格
        CellRangeAddress region2 =new CellRangeAddress(1, 2, 2, 16);
        sheet.addMergedRegion(region2);
        AtomicInteger ai3 = new AtomicInteger(3);
        Row row4 = sheet.createRow(ai3.getAndIncrement());
        cell = row4.createCell(0);
        cell.setCellValue("编号");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("时间带");
        cell = row4.createCell(2);
        cell.setCellValue("中途");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(3);
        cell.setCellValue("");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(4);
        cell.setCellValue("");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(5);
        cell.setCellValue("色漆1");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(6);
        cell.setCellValue("");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(7);
        cell.setCellValue("");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(8);
        cell.setCellValue("色漆2");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(9);
        cell.setCellValue("");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(10);
        cell.setCellValue("");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(11);
        cell.setCellValue("清漆");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(12);
        cell.setCellValue("");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(13);
        cell.setCellValue("");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(14);
        cell.setCellValue("计划停止");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(15);
        cell.setCellValue("ANDON入");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(16);
        cell.setCellValue("特记事项");
        cell.setCellStyle(cellStyle);
        CellRangeAddress region5 =new CellRangeAddress(3, 4, 0, 0);
        //编号合并单元格
        sheet.addMergedRegion(region5);
        setCellBorder(sheet, region5);
        CellRangeAddress region6 =new CellRangeAddress(3, 4, 1, 1);
        //合并单元格
        sheet.addMergedRegion(region6);
        setCellBorder(sheet, region6);
        CellRangeAddress region7 =new CellRangeAddress(3, 3, 2, 4);
        //合并单元格
        sheet.addMergedRegion(region7);
        setCellBorder(sheet, region7);
        CellRangeAddress region8 =new CellRangeAddress(3, 3, 5, 7);
        //合并单元格
        sheet.addMergedRegion(region8);
        setCellBorder(sheet, region8);
        CellRangeAddress region9 =new CellRangeAddress(3, 3, 8, 10);
        //合并单元格
        sheet.addMergedRegion(region9);
        setCellBorder(sheet, region9);
        CellRangeAddress region10 =new CellRangeAddress(3, 3, 11, 13);
        //合并单元格
        sheet.addMergedRegion(region10);
        setCellBorder(sheet, region10);
        CellRangeAddress region11 =new CellRangeAddress(3, 4, 14, 14);
        //合并单元格
        sheet.addMergedRegion(region11);
        setCellBorder(sheet, region11);
        CellRangeAddress region12 =new CellRangeAddress(3, 4, 15, 15);
        //合并单元格
        sheet.addMergedRegion(region12);
        setCellBorder(sheet, region12);
        CellRangeAddress region13 =new CellRangeAddress(3, 4, 16, 16);
        //合并单元格
        sheet.addMergedRegion(region13);
        setCellBorder(sheet, region13);
        //3.构造sheet
        String[] titles = {"编号", "时间带", "欠车","满车", "设备异常","欠车","满车","设备异常","欠车","满车","设备异常","欠车","满车","设备异常","计划停止","ANDON入","特记事项"};
        Row row5 =sheet.createRow(4);
        AtomicInteger ai4 = new AtomicInteger();
        for (String title : titles) {
            cell = row5.createCell(ai4.getAndIncrement());
            cell.setCellValue(title);
            //设置样式
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(5);
        int i = 1;
        Integer midwayOweCar = 0;//中途欠车
        Integer midwayFullCar = 0;//中途满车
        Integer midwayEquipmentBanormal = 0;//中途设备异常 MIDWAY_EQUIPMENT_BANORMAL
        Integer paint1OweCar = 0;//色漆1欠车
        Integer paint1FullCar = 0;//色漆1满车
        Integer paint1EquipmentBanormal = 0;//色漆1设备异常
        Integer paint2OweCar = 0;//色漆2欠车
        Integer paint2FullCar = 0;//色漆2满车
        Integer paint2EquipmentBanormal = 0;//色漆2设备异常
        Integer varnishOweCar = 0;//清漆欠车
        Integer varnishFullCar = 0;//清漆满车
        Integer varnishEquipmentBanormal = 0;//清漆设备异常
        Integer planStop = 0;//计划停止
        Integer andonIn = 0;//ANDON入
        if(list.size()>0){
            for (Spray report : list) {
                midwayOweCar = midwayOweCar+report.getMidwayOweCar();
                midwayFullCar = midwayFullCar+report.getMidwayFullCar();
                midwayEquipmentBanormal = midwayEquipmentBanormal + report.getMidwayEquipmentBanormal();
                paint1OweCar = paint1OweCar + report.getPaint1OweCar();
                paint1FullCar = paint1FullCar+report.getPaint1FullCar();
                paint1EquipmentBanormal = paint1EquipmentBanormal+report.getPaint1EquipmentBanormal();
                paint2OweCar = paint2OweCar + report.getPaint2OweCar();
                paint2FullCar = paint2FullCar + report.getPaint2FullCar();
                paint2EquipmentBanormal = paint2EquipmentBanormal+report.getPaint2EquipmentBanormal();
                varnishOweCar = varnishOweCar+report.getVarnishOweCar();
                varnishFullCar = varnishFullCar + report.getVarnishFullCar();
                varnishEquipmentBanormal = varnishEquipmentBanormal+report.getVarnishEquipmentBanormal();
                planStop = planStop+report.getPlanStop();
                andonIn = andonIn+report.getAndonIn();
                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(i++);
                cell.setCellStyle(cellStyle);
                //时间带
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getTimeZone());
                cell.setCellStyle(cellStyle);
                //中途欠车
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getMidwayOweCar());
                cell.setCellStyle(cellStyle);
                //中途满车
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getMidwayFullCar());
                cell.setCellStyle(cellStyle);
                //中途设备异常
                cell = dataRow.createCell(4);
                cell.setCellValue(report.getMidwayEquipmentBanormal());
                cell.setCellStyle(cellStyle);
                //色漆1欠车
                cell = dataRow.createCell(5);
                cell.setCellValue(report.getPaint1OweCar());
                cell.setCellStyle(cellStyle);
                //色漆1满车
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getPaint1FullCar());
                cell.setCellStyle(cellStyle);
                //色漆1设备异常
                cell = dataRow.createCell(7);
                cell.setCellValue(report.getPaint1EquipmentBanormal());
                cell.setCellStyle(cellStyle);
                //色漆2欠车
                cell = dataRow.createCell(8);
                cell.setCellValue(report.getPaint2OweCar());
                cell.setCellStyle(cellStyle);
                //色漆2满车
                cell = dataRow.createCell(9);
                cell.setCellValue(report.getPaint2FullCar());
                cell.setCellStyle(cellStyle);
                //色漆2设备异常
                cell = dataRow.createCell(10);
                cell.setCellValue(report.getPaint2EquipmentBanormal());
                cell.setCellStyle(cellStyle);
                //清漆欠车
                cell = dataRow.createCell(11);
                cell.setCellValue(report.getVarnishOweCar());
                cell.setCellStyle(cellStyle);
                //清漆满车
                cell = dataRow.createCell(12);
                cell.setCellValue(report.getVarnishFullCar());
                cell.setCellStyle(cellStyle);
                //清漆设备异常
                cell = dataRow.createCell(13);
                cell.setCellValue(report.getVarnishEquipmentBanormal());
                cell.setCellStyle(cellStyle);
                //计划停止
                cell = dataRow.createCell(14);
                cell.setCellValue(report.getPlanStop());
                cell.setCellStyle(cellStyle);
                //ANDON入
                cell = dataRow.createCell(15);
                cell.setCellValue(report.getAndonIn());
                cell.setCellStyle(cellStyle);
                //特记事项
                cell = dataRow.createCell(16);
                cell.setCellValue(report.getRemark());
                cell.setCellStyle(cellStyle);
            }
            AtomicInteger ai6 = new AtomicInteger(list.size()+5);
            Row row6 = sheet.createRow(ai6.getAndIncrement());
            cell = row6.createCell(0);
            cell.setCellValue(" TOTAL");
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(1);
            cell.setCellValue(0);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(2);
            cell.setCellValue(midwayOweCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(3);
            cell.setCellValue(midwayFullCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(4);
            cell.setCellValue(midwayEquipmentBanormal);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(5);
            cell.setCellValue(paint1OweCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(6);
            cell.setCellValue(paint1FullCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(7);
            cell.setCellValue(paint1EquipmentBanormal);
            cell.setCellStyle(cellStyle);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(8);
            cell.setCellValue(paint2OweCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(9);
            cell.setCellValue(paint2FullCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(10);
            cell.setCellValue(paint2EquipmentBanormal);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(11);
            cell.setCellValue(varnishOweCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(12);
            cell.setCellValue(varnishFullCar);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(13);
            cell.setCellValue(varnishEquipmentBanormal);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(14);
            cell.setCellValue(planStop);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(15);
            cell.setCellValue(andonIn);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(16);
            cell.setCellValue("");
            cell.setCellStyle(cellStyle);
            CellRangeAddress region14 =new CellRangeAddress(list.size()+5, list.size()+5, 0, 1);
            //合并单元格
            sheet.addMergedRegion(region14);
        }

        String fileName = URLEncoder.encode(sheet.getSheetName()+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }
    //合并单元格样式
    private void setCellBorder(Sheet sheet, CellRangeAddress cellAddresses) {
        RegionUtil.setBorderTop(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderLeft(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderRight(BorderStyle.MEDIUM, cellAddresses, sheet);


    }
}
